package fun.xinnix.config;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import entity.SysUser;
import fun.xinnix.common.RequestUserInfo;
import fun.xinnix.constants.RedisKeyConstant;
import fun.xinnix.constants.WebConstant;
import fun.xinnix.entity.Result;
import fun.xinnix.service.SysUserService;
import fun.xinnix.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Component
public class AuthFilter extends AuthenticatingFilter {
    @Resource
    RedisUtil redisUtil;
    @Resource
    SysUserService sysUserService;

    @Override
    protected AuthenticationToken createToken(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        String token = WebUtils.toHttp(servletRequest).getHeader(WebConstant.TOKEN_NAME);
        log.info("执行了========》createToken");
        log.info("token:{}", token);
        return new AuthToken(token);
    }

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        log.info("执行了========》isAccessAllowed");
        if (((HttpServletRequest) request).getMethod().equals(RequestMethod.OPTIONS.name())) {
            return true;
        }
        return super.isAccessAllowed(request, response, mappedValue);
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        log.info("执行了========》onAccessDenied");
        String token = WebUtils.toHttp(servletRequest).getHeader(WebConstant.TOKEN_NAME);
        if (StrUtil.isBlank(token)) {
            setResponse(servletResponse, JSONUtil.toJsonStr(Result.error("登录denied")));
            return false;
        }
        return executeLogin(servletRequest, servletResponse);
    }

    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
        setResponse(response, JSONUtil.toJsonStr(Result.error("登录失败")));
        return false;
    }

    private void setResponse(ServletResponse response, String jsonMsg) {
//        response.set(HttpStatus.FORBIDDEN.value());
        response.setContentType("text/json");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().println(jsonMsg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
